package cn.milolab.djback.service;

import cn.milolab.djback.bean.entity.Employee;
import cn.milolab.djback.bean.entity.JobRecord;
import cn.milolab.djback.bean.response.SalaryResponse;
import cn.milolab.djback.dao.EmployeeDAO;
import cn.milolab.djback.dao.JobRecordDAO;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static com.google.common.base.Preconditions.checkArgument;
/**
 * @author 叶欣委
 */
@Service
public class SalaryService {

    @Autowired
    JobRecordDAO jobRecordDAO;
    @Autowired
    EmployeeDAO employeeDAO;

    /**
     * 根据时间计算工资
     * @param startTime
     * @param endTime
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public List<SalaryResponse> getSalary(Date startTime, Date endTime) {
        checkArgument(startTime != null && endTime != null,"参数不能为空");
        //根据选择时间找出所有的值班记录
        List<JobRecord> jobRecordList = jobRecordDAO.getJobRecordByTime(startTime,endTime);
        //对值班记录按照employeeId进行分组
        Map<Integer,List<JobRecord>> jobRecordMap = jobRecordList.stream()
                .collect(Collectors.groupingBy(JobRecord::getEmployeeId));

        List<SalaryResponse> res = new ArrayList<>();
        jobRecordMap.forEach((key, value) -> {
            SalaryResponse salaryResponse = new SalaryResponse();
            //根据employeeId找到对应的employee对象信息
            Employee employee = employeeDAO.getEmployeeById(key);
            //设置姓名
            salaryResponse.setName(employee.getName());
            //找出这个人的排班的记录
            List<JobRecord> arrangeJobRecordList = value.stream()
                    .filter(jobRecord -> jobRecord.getIsArrange())
                    .collect(Collectors.toList());
            //计算出所有排班的时间总和
            double arrangeTime = arrangeJobRecordList.stream()
                    .mapToDouble(arrangeJobRecord -> getJobTime(arrangeJobRecord))
                    .sum();
            //设置排班时间
            salaryResponse.setArrangedWorkTime(arrangeTime);
            //找出这个人的抢班记录
            List<JobRecord> nonArrangeJobRecordList = value.stream()
                    .filter(jobRecord -> !jobRecord.getIsArrange())
                    .collect(Collectors.toList());
            //计算出所有的抢班时间总和
            double nonArrangeTime = nonArrangeJobRecordList.stream()
                    .mapToDouble(nonArrangeJobRecord -> getJobTime(nonArrangeJobRecord))
                    .sum();
            //设置抢班时间
            salaryResponse.setWorkTime(nonArrangeTime);
            Integer arrangeSalary = 6;
            Integer nonArrangeSalary = 8;
            //计算工资
            salaryResponse.setSalary((int) Math.floor(arrangeSalary*arrangeTime+nonArrangeSalary*nonArrangeTime));
            res.add(salaryResponse);
        });
        return res;
    }

    /**
     * 计算一个值班记录的工作时间
     * @param jobRecord
     * @return
     */
    private Double getJobTime(JobRecord jobRecord) {
        double nd = 1000 * 24 * 60 * 60;
        double nh = 1000 * 60 * 60;
        //计算出相差多少毫秒
        double milliSecond = jobRecord.getEndTime().getTime() - jobRecord.getStartTime().getTime();
        //计算出相差多少小时
        double hour = milliSecond % nd / nh;
        return hour;
    }
}


